package cn.ycxy.lbl.posts.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author BaoL
 * @Date 2023/3/12 19:28
 * @Description 扩展 自动填充插入与更新时的时间
 * @Since version-1.0
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("insert 自动对时间填充...");
        LocalDateTime dateTime = LocalDateTime.now();
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, dateTime);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, dateTime);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("update 自动对时间填充...");
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
